package com.rabbitMq.consumer.service;


import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Service
@RabbitListener(queues = "TestDirectQueue1")//监听的队列名称 TestDirectQueue1 ,多个队列创建多个 Service 即可
public class RabbitMqListenerService {

    private AtomicInteger successCount = new AtomicInteger();
    private AtomicInteger failCount = new AtomicInteger();

    @RabbitHandler
    public void process(String testMessage) {
        System.out.println("TestDirectQueue1队列收到消息  : " + testMessage);
        try {
            System.out.println(testMessage);
            successCount.addAndGet(1);
        } catch (Exception e) {
            failCount.addAndGet(1);
        }
    }

    public Map<String, Object> getReceivingQuantity() {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("ServiceSuccessCount", successCount);
        dataMap.put("ServiceFailCount", failCount);
        return dataMap;
    }
}
